part of _;

extension on ProductListPage {
  Widget view_topBar() {
    return Container(
      width: yScreenSize().width,
      height: yStatubarHeight() + yNavBarHeight(),
      decoration: BoxDecoration(image: DecorationImage(image: AssetImage(YConfig.navBar), fit: BoxFit.fill)),
      child: Padding(
        padding: EdgeInsets.only(top: yStatubarHeight()),
        child: Row(
          children: [
            YButton(width: yNavBarHeight(), height: yNavBarHeight(), child: Icon(Icons.arrow_back_ios), onTap: () => yFinish()),
            Expanded(
              child: GestureDetector(
                onTap: () => yIntent(SearchPage(), intentType: YIntentType.Present),
                child: Container(
                  padding: EdgeInsets.only(left: 10, right: 10),
                  margin: EdgeInsets.only(top: 5, bottom: 5),
                  decoration: BoxDecoration(borderRadius: BorderRadius.circular(99), border: Border.all(color: YConfig.themeColor, width: 1.5), color: Colors.white),
                  alignment: Alignment.centerLeft,
                  child: Text(searchContent, maxLines: 1, overflow: TextOverflow.ellipsis),
                ),
              ),
            ),
            YButton(
              width: yNavBarHeight(),
              height: yNavBarHeight(),
              child: Icon(IFinal.Others.isProductShowGrid ? Icons.grid_view : Icons.format_list_bulleted),
              onTap: () => AppUtils.toggleProductShowType(yPage),
            ),
          ],
        ),
      ),
    );
  }

  Widget view_search(ScrollController scrollController) {
    double minWidth = 120, space = 5;
    double singleWidth = (yScreenSize().width - (searchItems.length + 1) * space) / searchItems.length;
    bool isScrollable = !(searchItems.length < (singleWidth > minWidth ? 4 : 3));
    double width = isScrollable ? minWidth : singleWidth;
    double radius = (searchBarHeight - space * 2) / 2;
    return Container(
      color: IFinal.Colors.pageBG_navbar,
      child: Container(
        height: searchBarHeight,
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10))),
        padding: EdgeInsets.fromLTRB(space, space, 0, 0),
        child: Row(
          children: [
            Expanded(
              child: ListView.builder(
                padding: EdgeInsets.zero,
                controller: scrollController,
                physics: isScrollable ? null : NeverScrollableScrollPhysics(),
                scrollDirection: Axis.horizontal,
                itemCount: searchItems.length,
                itemBuilder: (context, index) {
                  var item = searchItems[index];
                  var isUsed = item.value.isNotEmpty;
                  return Column(
                    children: [
                      YButton(
                        width: width,
                        height: searchBarHeight - space * 2,
                        margin: EdgeInsets.only(right: space),
                        borderRadius: item.dialogContext == null ? BorderRadius.circular(radius) : BorderRadius.only(topLeft: Radius.circular(radius), topRight: Radius.circular(radius)),
                        decoration: BoxDecoration(color: Colors.grey.shade100),
                        tapTime: 0,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              constraints: BoxConstraints(maxWidth: minWidth / 2),
                              padding: EdgeInsets.only(left: 10),
                              child: Text(isUsed ? item.value : item.title, style: TextStyle(color: isUsed ? YConfig.themeColor : Colors.grey), overflow: TextOverflow.ellipsis),
                            ),
                            Icon(item.dialogContext != null ? Icons.arrow_drop_up : Icons.arrow_drop_down, color: isUsed ? YConfig.themeColor : Colors.grey),
                          ],
                        ),
                        onTap: () => controller_showSearchPopup(item, scrollController),
                      ),
                      Container(width: width, height: space, margin: EdgeInsets.only(right: space), color: item.dialogContext == null ? Colors.transparent : Colors.grey.shade100),
                    ],
                  );
                },
              ),
            ),
            !isScrollable ? YEmpty() : Icon(Icons.chevron_right, color: Colors.grey, size: 20),
          ],
        ),
      ),
    );
  }

  Widget view_single(StateSetter setDialogState, SearchMainModel item) {
    return ListView.builder(
      padding: EdgeInsets.fromLTRB(5, 5, 5, 0),
      primary: false,
      shrinkWrap: true,
      itemCount: item.items.length,
      itemBuilder: (context, index) {
        var subItem = item.items[index];
        return Card(
          elevation: 1,
          margin: EdgeInsets.only(bottom: 5),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
          child: YButton(
            onTap: () {
              item.value = subItem.title;
              item.items.forEach((element) => element.isSelect = false);
              subItem.isSelect = true;
              controller_refreshSearchBar();
              controller_closeSearchPopup(item);
              controller_initData(false);
            },
            enable: !subItem.isSelect,
            height: 45,
            borderRadius: BorderRadius.circular(5),
            decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(5), border: subItem.isSelect ? Border.all(color: YConfig.themeColor) : null),
            child: Padding(
              padding: EdgeInsets.only(left: 10, right: 10),
              child: Row(
                children: [
                  Text(subItem.title, style: TextStyle(color: subItem.isSelect ? YConfig.themeColor : Colors.black)),
                  YExpanded(),
                  subItem.isSelect ? Icon(Icons.check_rounded, color: YConfig.themeColor) : YEmpty(),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget view_mutiplie(StateSetter setDialogState, SearchMainModel item) {
    return Container(
      height: yScreenSize().height / 2,
      child: Column(
        children: [
          Expanded(
            child: GridView.builder(
              padding: EdgeInsets.all(5),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, mainAxisSpacing: 5, crossAxisSpacing: 5, childAspectRatio: 5),
              itemCount: item.items.length,
              itemBuilder: (context, index) {
                var subItem = item.items[index];
                return YButton(
                  onTap: () => setDialogState(() => subItem.isSelect = !subItem.isSelect),
                  child: Padding(
                    padding: EdgeInsets.only(left: 10, right: 10),
                    child: Row(
                      children: [
                        Text(subItem.title, style: TextStyle(color: subItem.isSelect ? YConfig.themeColor : Colors.black)),
                        YSpacer(width: 10),
                        subItem.isSelect ? Icon(Icons.check_rounded, color: YConfig.themeColor, size: 20) : YEmpty(),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
          Container(
            decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10))),
            padding: EdgeInsets.all(5),
            child: Row(
              children: [
                Expanded(
                  child: YButton(
                    height: 40,
                    child: Text("重置", style: TextStyle(color: Colors.white)),
                    borderRadius: BorderRadius.circular(20),
                    decoration: BoxDecoration(color: IFinal.Colors.themeColor2),
                    onTap: () {
                      setDialogState(() => item.items.forEach((element) => element.isSelect = false));
                      controller_refreshSearchBar();
                      controller_closeSearchPopup(item);
                      controller_initData(false);
                    },
                  ),
                ),
                YSpacer(width: 5),
                Expanded(
                  child: YButton(
                    height: 40,
                    child: Text("确定", style: TextStyle(color: Colors.white)),
                    borderRadius: BorderRadius.circular(20),
                    decoration: BoxDecoration(color: YConfig.themeColor),
                    onTap: () {
                      controller_refreshSearchBar();
                      controller_closeSearchPopup(item);
                      controller_initData(false);
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
